// pages/machining/machining.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    factory: [{
      Name: '请选择',
      id: ''
    }],
    factoryIndex: 0,
    warehouse: [{
      realName: '请选择',
      id: ''
    }],
    warehouseIndex: 0,
    businessList: "",
    clothingStyleList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    this.getbusinessList()
    // this.getpurchasingList()
    this.userList()
    this.warehouseList()
  },
  getbusinessList: function() {
    var that = this
    wx.request({
      url: app.globalData.api + 'businessOrderForm/' + this.data.id,
      data: {},
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 200) {
          let list = res.data.data
          that.setData({
            businessList: res.data.data
          })
        }
      }
    })
  },
  // getpurchasingList: function() {
  //   var that = this
  //   wx.request({
  //     url: app.globalData.api + 'purchasingOrderForm/list',
  //     data: {
  //       "keyword ": "",
  //       "orderBy": "",
  //       "status": "审核通过",
  //       "purchasingManagerUserId": "",
  //       "timeRange": "",
  //       "page": "",
  //       "rows": 99
  //     },
  //     method:'POST',
  //     header: {
  //       Authorization: wx.getStorageSync('token')
  //     },
  //     success: (res) => {
  //       console.log(res)
  //       if (res.data.code == 200) {
  //         for (var i in res.data.data.purchasingOrderFormList){
  //           that.data.purchase.push({
  //             orderName: res.data.data.purchasingOrderFormList[i].orderName,
  //             id: res.data.data.purchasingOrderFormList[i].id,
  //           })
  //         }

  //         that.setData({
  //           purchase: that.data.purchase
  //         })
  //       }
  //     }
  //   })
  // },
  userList: function() {
    var that = this
    wx.request({
      url: app.globalData.api + 'factoryGroup/list',
      data: {
        page: 1,
        rows: 999,

      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          for (var i in res.data.data.factoryGroupList) {
            that.data.factory.push({
              Name: res.data.data.factoryGroupList[i].groupName,
              id: res.data.data.factoryGroupList[i].id,
            })
          }

          that.setData({
            factory: that.data.factory
          })
        }
      }
    })
  },
  warehouseList() {
    var that = this
    wx.request({
      url: app.globalData.api + 'warehouse/findWarehouseList',
      data: {

      },
      method: 'GET',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          for (var i in res.data.data) {
            that.data.warehouse.push(res.data.data[i])
          }

          that.setData({
            warehouse: that.data.warehouse
          })
        }
      }
    })
  },
  sub: function(e) {
    console.log(e)
  
    if (this.data.factory[this.data.factoryIndex].id == '') {
      wx.showToast({
        icon: 'none',
        title: '请填写完整信息',
      })
      return
    }
    if (this.data.warehouse[this.data.warehouseIndex].id == '') {
      wx.showToast({
        icon: 'none',
        title: '请填写完整信息',
      })
      return
    }
    wx.request({
      url: app.globalData.api + 'productionOrderForm/add',
      data: {
        businessOrderFormId:this.data.id,
        // purchasingOrderFormId: this.data.purchase[this.data.purchaseIndex].id,
        factoryGroupId: this.data.factory[this.data.factoryIndex].id,
        warehouseId: this.data.warehouse[this.data.warehouseIndex].id,
        clothingStyleList: this.data.clothingStyleList
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.msg,
          })
          setTimeout(()=>{
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.errorMsg,
          })
        }
      }
    })
  },
  mach: function(e) {
    var that = this;
    var bean = {};
    var isExsist = false;
    bean.id=e.currentTarget.dataset.id;
    // console.log(bean.id)
    bean.preOrderQuantity=e.detail.value;
    if (that.data.clothingStyleList.length==0){
      that.data.clothingStyleList.push(bean);
    }else{
      for (var i = 0; i < that.data.clothingStyleList.length;i++){
        if (that.data.clothingStyleList[i].id == bean.id){     
          that.data.clothingStyleList[i]=bean;
          isExsist = true;
        }
        if (i == (that.data.clothingStyleList.length - 1) && !isExsist){
          that.data.clothingStyleList.push(bean);
        }          
      }
    }
    
    console.log(that.data.clothingStyleList);

    // this.data.list.clothingStyleList[e.currentTarget.dataset.index].preOrderQuantity = e.detail.value
  },
  factoryChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      factoryIndex: e.detail.value
    })
  },
  purchaseChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      warehouseIndex: e.detail.value
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})